package org.emcode.sample.appleguice.client.mvp;

import org.emcode.sample.appleguice.shared.event.GreetingSentEvent;
import org.emcode.sample.appleguice.shared.event.GreetingSentEventHandler;

import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.place.Place;
import net.customware.gwt.presenter.client.place.PlaceRequest;
import net.customware.gwt.presenter.client.widget.WidgetDisplay;
import net.customware.gwt.presenter.client.widget.WidgetPresenter;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasHTML;
import com.google.gwt.user.client.ui.HasText;
import com.google.inject.Inject;

public class GreetingResponsePresenter extends WidgetPresenter<GreetingResponsePresenter.Display> {
	public interface Display extends WidgetDisplay {
		
		HasText getTextToServer();

		HasHTML getServerResponse();

		HasHTML getVisitorCount();
		
		// trying to move from dialog box to composite widget
		// HasClickHandlers getClose();
		// DialogBox getDialogBox();
	}

	public static final Place PLACE = new Place("GreetingResponse");

	@Inject
	public GreetingResponsePresenter(final Display display, final EventBus eventBus) {
		super(display, eventBus);

		bind();
	}
	
	@Override
	protected void onBind() {

		/*
		display.getClose().addClickHandler(new ClickHandler() {
			public void onClick(final ClickEvent event) {
				display.getDialogBox().hide();
			}
		});
		*/
		
		eventBus.addHandler(GreetingSentEvent.TYPE, new GreetingSentEventHandler() {

			public void onGreetingSent(final GreetingSentEvent event) {
				Log.info("Handling GreetingSent event in GreetingResponsePresenter");
				
				display.getTextToServer().setText(event.getName());
				display.getServerResponse().setHTML(event.getMessage());
				display.getVisitorCount().setHTML("Visitor count:" + event.getCount().toString());
				
				
				Log.info(display.getTextToServer().getText());
				Log.info(display.getServerResponse().getText());
				
				// make sure we can see it, since it is turned off when a 
				// user first gets to the page
				display.asWidget().setVisible(true);
				display.asWidget().setStyleName("centered-panel");
				
			}
		});
	}

	@Override
	protected void onUnbind() {
		// Add unbind functionality here for more complex presenters.
	}

	public void refreshDisplay() {
		// This is called when the presenter should pull the latest data
		// from the server, etc. In this case, there is nothing to do.
	}

	public void revealDisplay() {
		// Nothing to do. This is more useful in UI which may be buried
		// in a tab bar, tree, etc.
	}

	/**
	 * Returning a place will allow this presenter to automatically trigger when
	 * '#GreetingResponse' is passed into the browser URL.
	 */
	@Override
	public Place getPlace() {
		return PLACE;
	}

	@Override
	protected void onPlaceRequest(final PlaceRequest request) {
		// this is a popup
	}
}